/**
 * 
 */
package br.com.pim.action;

import java.sql.Connection;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import br.com.pim.connection.ConnectionFactory;
import br.com.pim.dao.UsuarioDAO;
import br.com.pim.form.UsuarioForm;
import br.com.pim.model.UsuarioModel;

/**
 * @author douglas.peixoto
 * 
 */
public class UsuarioAction extends DispatchAction {

	public ActionForward iniciar(ActionMapping mapping, ActionForm form,
			HttpServletRequest req, HttpServletResponse resp) throws Exception {
				
			Connection myConn = null;
			UsuarioDAO uDAO = null;
			
				try{
					myConn = new ConnectionFactory().getConnectionMySQL();
					uDAO = new UsuarioDAO(myConn);
					req.setAttribute("LISTA_USUARIOS", uDAO.buscarTodos());
					
				}finally{
					if(!(myConn == null)){
						myConn.close();
					}
				}
		
			return mapping.getInputForward();

	}

	
	public ActionForward inserir(ActionMapping mapping, ActionForm form,
			HttpServletRequest req, HttpServletResponse resp) throws Exception {
				Connection myConn = null;
				UsuarioForm uForm = (UsuarioForm) form;
				UsuarioModel uModel = null;
				UsuarioDAO uDAO = null;
				
					try{
						myConn = new ConnectionFactory().getConnectionMySQL();
						uDAO = new UsuarioDAO(myConn);
						uModel = uForm.getUsuario();
						
						if(uForm.getUsuario().getVrSenha().equals(uForm.getConfirmaSenha())){
							uDAO.inserir(uModel);
						}else {
								return null;
						}
						
						this.iniciar(mapping, uForm, req, resp);
					}finally{
						if(!(myConn==null)){
							myConn.close();
						}
					}
		
		
		
		
			return mapping.getInputForward();
		
		
		
	}
}
